package mya_dc.shared_classes;

import java.io.File;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import mya_dc.shared_classes.Message.EMessageType;
import mya_dc.shared_classes.gui.GetAcrobatReaderDialog;

/**
 * Shared definitions for multiple use
 * 
 * @author MYA
 *
 */
public abstract class SharedDefinitions 
{
	// global constants
	public static final int ClientToCompServerPort = 5555;
	public static final int DefaultRetryNumber = 3;
	public static final int SecsBetweenRetries = 3;
	public static final int SecsBetweenTwoKeepAliveMessages = 1;
	public static final int MiliSecondsWhenConnectingToRemote = 1000;
	public static final String HelpFilePath = System.getProperty("user.dir") + "\\Help\\MYA_Help\\mya_help.pdf";
	public static String AcrobatReaderFile = "C:\\Program Files\\Adobe\\Reader 8.0\\Reader\\AcroRd32.exe";
	
	// Entities enumeration 
	public static enum Entity {
		ENTITY_MASTER,
		ENTITY_SLAVE,
		ENTITY_COMPILATION,
		ENTITY_CLIENT
	}
	
	/**
	 * 
	 * 
	 * @param transmitter - the relevant transmitter
	 * @param expectedEntity - the expected entity 
	 * @param closeTransmitterIfBadEntity - true if socket should be closed if not
	 * @return true if expected entity
	 */
	public static boolean isExpectedEntity(
			Transmitter transmitter,
			Entity expectedEntity,
			boolean closeTransmitterIfBadEntity
			)
	{
		try
		{
			Message ID = transmitter.receive();
			if (ID.getType() != EMessageType.LOGIN_ENTITY_IDENTIFICATION)
				throw new IOException();
			if (ID.getObject() instanceof Entity) {
				{
					Entity ent = (Entity) ID.getObject();
					if (ent !=  expectedEntity)
						throw new IOException();
				}
			}
		}
		catch (Exception e)
		{
			if (closeTransmitterIfBadEntity)
			{
				transmitter.close();
				return false;
			}
			return false;
		}
		return true;
	}
	
	/**
	 * opens the help file
	 * 
	 * @param owner - the frame that initiated the open.
	 * 
	 */
	public static void openHelpFile(JFrame owner)
	{
		String launchString = AcrobatReaderFile + " \"" + HelpFilePath + "\"";
		File f = new File(AcrobatReaderFile);
		while (!f.exists())
		{
			GetAcrobatReaderDialog window = new GetAcrobatReaderDialog(AcrobatReaderFile);
			window.setModal(true);
			window.setVisible(true);
			if (window.canceled)
				return;
			SharedDefinitions.AcrobatReaderFile = window.m_location;
			f = new File(AcrobatReaderFile);
			launchString = AcrobatReaderFile + " \"" + HelpFilePath + "\"";
		}
		try
		{
			Runtime.getRuntime().exec(launchString);
		}
		catch (IOException e)
		{
			JOptionPane.showMessageDialog(owner, "Error loading MYA help file");
		}
	}
	
	/**
	 * Validates an I.P. address
	 * 
	 * @param sip - string of I.P. address
	 * @return true if structure is OK
	 */
	public static boolean legalIP (String sip)
	{
	    String [] parts = sip.split ("\\.");
	    for (String s : parts)
	    {
	        int i = Integer.parseInt (s);
	        if (i < 0 || i > 255)
	        {
	            return false;
	        }
	    }
	    return true;
	}
}
